<?php
/**
 * --------------------------------------------------------------
 * HTTP类函数库
 * Author: zd <kfzphp@126.com>
 * Date: 2018-06-01
 * Project: kfzphp.com
 * --------------------------------------------------------------
 */

/**
 * 获取POST参数
 * @param  [type]     $value   [post请求值]
 * @param  string     $default [为空返回默认值]
 */
function post($value, $default = '')
{
    if (IS_POST) {
        return !empty($_POST[$value]) ? filter($_POST[$value]) : $default;
    }
    return false;
}

/**
 * 跳入链接
 * @param  [type]     $url  [跳转链接，例'mobile/login/logout']
 * @param  [type]     $exit [默认不停止]
 * @return [type]           [description]
 */
function redirect_url($url = '', $exit = false)
{
    if ($url) {
        $exp = array_values(array_filter(explode('/', $url)));
        $module = strtolower($exp[0]);
        $controller = ucfirst($exp[1]) . 'Controller';
        $action = strtolower($exp[2]);
        $file = MODULE_PATH . '/' . $module . '/controller/' . $controller . '.class.php';
        if (file_exists($file)) {
            require_once $file;
            // $included_files = get_included_files();
            // pt($included_files);
            if (class_exists($controller)) {
                $obj = new $controller();
                if (method_exists($obj, $action)) {
                    $obj->$action();
                }
            }
        }
        if ($exit) {
            exit;
        }
    }
}

/**
 * 跳转至URL
 * @param  string     $url [跳转链接，例'/mobile/login/logout']
 * @return [type]          [description]
 */
function location_url($url = '')
{
    if ($url) {
        header('Location: ' . $url);
        exit;
    }
}

/**
 * curl get
 * @param  [type]     $url [description]
 * @return [type]          [description]
 */
function get_request($url)
{
    $ch = curl_init();
    // $header = 'Accept-Charset: utf-8';
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $temp = curl_exec($ch);
    curl_close($ch);
    return $temp;
}

/**
 * curl post
 * @param  [type]     $url [description]
 * @return [type]          [description]
 */
function post_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

/**
 * 获取客户端IP
 */
function client_ip()
{
    $cip = "unknown";
    if($_SERVER['REMOTE_ADDR']) {
        $cip = $_SERVER['REMOTE_ADDR'];
    } elseif (getenv("REMOTE_ADDR")) {
        $cip = getenv("REMOTE_ADDR");
    }
    return $cip;
}
